/*
 * SocketRead.h
 *
 *  Created on: Dec 21, 2011
 *      Author: dzm
 */

#include <fstream>

#include <QHostAddress>
#include <QtCore>
#include <QTcpSocket>
#include <qthread.h>
#include <QElapsedTimer>
#include <sys/time.h>
#include <algorithm>
#include <list>

#include "Packet.h"
#include "Route.h"
#include "SemanticUtil/RdfParser.h"
#include "PacketUtil/PacketForSem.h"
#include "NetworkUtil/Node.h"
#include "SemanticUtil/QueryParser.h"
#include "SemanticUtil/QueryPacket.h"
#include "SemanticUtil/ResultHandler.h"
#include "SemanticHandler/ReasonPacket.h"
#include "SemanticHandler/RDFSProcessor/structure/Predicate.h"
#include "SemanticHandler/RDFSProcessor/structure/Term.h"
#include "SemanticHandler/RDFSProcessor/RDFSUtil.h"
#include "SemanticHandler/RDFSProcessor/RDFSProcessor.h"
#include <unistd.h>
#include <sqlite3.h>
#include <time.h>
#include "FwdInfer/FwdRule.h"
#include "FwdInfer/Statement.h"
#include "FwdInfer/InferenceModel.h"


#ifndef SOCKETREAD_H_
#define SOCKETREAD_H_

class SocketRead : public QThread {

	 Q_OBJECT

public:
	SocketRead();
	SocketRead(QObject* parent );
	SocketRead(int ID, QObject* parent, int inport, Node localnode);
	SocketRead(int ID, int inport, Node& localnode);
	std::string iToString(int i);
	std::vector<NTuple> getAnswer(std::string ans);
	std::vector<NTuple> project(std::vector<std::string> target,std::string commonVar, std::vector<NTuple> ans, std::vector<Binding> subs );
	std::vector<NTuple> delCommon(std::vector<NTuple> ans, std::vector<std::string> target);
	virtual ~SocketRead();
	void set(int ID);
	void run();
	void toFile();

signals:
	void error(QTcpSocket::SocketError socketerror);

public slots:
	void readPacket();
	void disconnect();

private:
	int socketDescriptor;
	Node localnode_sr;
	Route route_sr;
	QTcpSocket* socket;
	int inport_sr;
	int outport_sr;
	Packet packetReceive_sr;
	quint16 nextBlockSize;
	timeval timerstart,timerend;
	int timeuse;
	QString timeFile;
};

#endif /* SOCKETREAD_H_ */
